<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Tools\Func;
use App\Models\AdminUser;
use App\Models\KeShi;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;

use App\Imports\FeiYongImport;

class KeShiController extends Controller
{
    use Func;

    // 科室列表 分页
    public function keShiList(Request $request)
    {
        $keywords = trim($request->keywords);
		
        $data = KeShi::where(function ($query) use($keywords) {
            // 关键字
            if ($keywords) {
                $query->orWhere('keshi_name', $keywords);
            }
        });
		if ($request->type ?? 0) {
			$data = $data->get();
		} else {
			$data = $data->paginate($request->limit ?? 10);
		}
		
        return $this->returnJson(0, 'success', $data);
    }
	
	
	
	//修改状态
	public function editStatus(Request $request)
	{
		if (!KeShi::where(['id' => $request->id])->update(['status' => $request->status])) {
			
            return $this->returnJson(1, '修改失败');
        }

        return $this->returnJson(0, '修改成功');
	}
	

    // 添加合同
    public function addKeshi(Request $request)
    {
        if (!$res = KeShi::create([
            'keshi_name' => $request->keshi_name,
            'created_at' => date('Y-m-d H:i:s')
        ])) {
            return $this->returnJson(1, '添加失败', $res);
        }

        return $this->returnJson(0, '添加成功', $res);
    }

    // 删除合同
    public function delKeshi(Request $request)
    {
        if (!KeShi::where('id', $request->id)->delete()) {
            return $this->returnJson(1, '删除失败');
        }

        return $this->returnJson(0, '删除成功');
    }

    // 获得科室信息
    public function getKeshiInfo(Request $request)
    {

        $res = KeShi::where('id', $request->id)->first();

        return $this->returnJson(0, 'success', $res);
    }

    // 编辑合同信息
    public function editKeshi(Request $request)
    {
        // dd($request->addAccount);
        if (!$res = KeShi::where('id', $request->id)->update([
            'keshi_name' => $request->keshi_name
        ])) {
            return $this->returnJson(1, '修改失败', $res);
        }

        return $this->returnJson(0, '修改成功', $res);       
    }

}
